from django.shortcuts import render, HttpResponse, redirect

from .models import Topic, Entry

from .forms import TopicForm, EntryForm

from django.contrib.auth.decorators import login_required

from django.http import Http404


# Create your views here.

def index(request):
    """学习笔记的主页"""
    return render(request, 'learning_logs/index.html')


@login_required
def topics(request):  # 添加装饰器 限制访问
    """显示所有的主题"""
    topics = Topic.objects.filter(owner=request.user).order_by('date_added')
    context = {'topics': topics}
    return render(request, 'learning_logs/topics.html', context)


@login_required
def topic(request, topic_id):
    """显示单个主题及所有的条目"""
    topic = Topic.objects.get(id=topic_id)
    # 确认请求的主题属于当前的用户
    check_topic_owner(request, topic)
    entries = topic.entry_set.order_by('-date_added')
    context = {'topic': topic, 'entries': entries}
    return render(request, 'learning_logs/topic.html', context)


@login_required
def new_topic(request):
    """添加新主题"""
    if request.method != 'POST':
        # 未提交数据 创建一个新表单
        form = TopicForm()
    else:
        # POST提交的数据；对数据进行处理
        form = TopicForm(data=request.POST)
        if form.is_valid():  # 进行校验
            new_topic_1 = form.save(commit=False)  # 添加新主题
            new_topic_1.owner = request.user  # 让新主题的owner设置成当前的用户
            new_topic_1.save()  # 数据有效保存到数据库
            return redirect('learning_logs:topics')
    # 显示空表单或指出表单数据无效
    context = {'form': form}
    return render(request, 'learning_logs/new_topic.html', context)


@login_required
def new_entry(request, topic_id):
    """在特定主题中添加新条目"""
    topic = Topic.objects.get(id=topic_id)
    if request.method != 'POST':
        # 未提交数据 创建一个空表单
        form = EntryForm()
    else:
        # POST提交数据 对数据进行处理
        form = EntryForm(data=request.POST)
        if form.is_valid():
            new_entry = form.save(commit=False)
            new_entry.owner = request.user
            new_entry.topic = topic
            new_entry.save()
            return redirect('learning_logs:topic', topic_id=topic_id)
    # 显示空表表单或指出表单数据无效
    context = {'topic': topic, 'form': form}
    return render(request, 'learning_logs/new_entry.html', context)


@login_required
def edit_entry(request, entry_id):
    """编辑现有的条目"""
    entry = Entry.objects.get(id=entry_id)
    topic = entry.topic
    check_topic_owner(request, topic)
    if request.method != 'POST':
        # 第一次请求 使用当前的条目填充
        form = EntryForm(instance=entry)
    else:
        # POST提交数据 对数据进行处理
        form = EntryForm(instance=entry, data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('learning_logs:topic', topic_id=topic.id)
    context = {'entry': entry, 'topic': topic, 'form': form}
    return render(request, 'learning_logs/edit_entry.html', context)


def check_topic_owner(request, topic1):
    if topic1.owner != request.user:
        raise Http404


def entry_delete(request):
    """删除内容"""
    nid = request.GET.get('nid')
    nid = Entry.objects.get(id=nid)
    topic = nid.topic  # 获取到他的当前页面的id
    nid.delete()
    return redirect('learning_logs:topic', topic_id=topic.id)


def topics_delete2(request):
    """删除标题"""
    nid = request.GET.get('nid')
    nid = Topic.objects.get(id=nid)
    nid.delete()
    return redirect('learning_logs:topics')
